$(document).ready (function() {
  var oAutoCompProduct;
  
  
  var resultCallbackProduct = function(sRequest, oResponse, oPayload) {
    myProductDataTable.onDataReturnInitializeTable(sRequest, oResponse, oPayload);
	myProductDataTable.selectRow(0);
  };
  
  var getProductResults = function(query, last) {
	  myDataSourceProduct.sendRequest('query=' + query, 
	resultCallbackProduct, myProductDataTable);
  };

  var oACDSProduct = new YAHOO.widget.DS_JSFunction(getProductResults);
  oACDSProduct.maxCacheEntries = 0;
  oAutoCompProduct = new YAHOO.widget.AutoComplete("dt_input","dt_ac_container", oACDSProduct);
  oAutoCompProduct.minQueryLength = 0;
  var myColumnHeadersProduct = [{key:"id", label: "id", sortable: false, height: '2em', width:'50'}, 
	                     {key:"name", label: "name", sortable: false, height: '2em', width:'350'},
	                     {key:"brand", label: "brand", sortable: false, height: '2em', width:'50'},
	                     {key:"category", label: "category", sortable: false, height: '2em', width:'50'},
	                     {key:"price", label: "price", sortable: false, height: '2em', width:'50'},
	                     {key:"stock", label: "stock", sortable: false, height: '2em', width:'50'}];
  var myColumnSetProduct = new YAHOO.widget.ColumnSet(myColumnHeadersProduct);
  var myDataSourceProduct = new YAHOO.util.DataSource(baseweb + "/module/product/searchProduct.do?");
  myDataSourceProduct.responseType = YAHOO.util.DataSource.TYPE_JSON;
  myDataSourceProduct.maxCacheEntries = 50;
  myDataSourceProduct.responseSchema = {
	          resultsList: "Result",
	          fields: ["id", "name", "brand", "category", "price", "stock"]
	        };
	
 var oConfigsProduct = {
	          initialRequest: "query=", 
	          selectionMode:"single",
	          scrollable:true,
	          height:"20em",
	          width:"600px"
	        };
 function rowSelectedProduct(oArgs) {
	          target = oArgs.target;
	          id = target.getElementsByClassName('yui-dt-col-id')[0].children[0].innerHTML;
	          name = target.getElementsByClassName('yui-dt-col-name')[0].children[0].innerHTML;
	          price = target.getElementsByClassName('yui-dt-col-price')[0].children[0].innerHTML;
	          addProductToResultProduct(id, name, price);
	          myProductDataTable.onEventSelectRow(oArgs);
	          $('#myProductTable').toggle(); 
	        }
 YAHOO.widget.DataTable.MSG_LOADING = "Something";
 var myProductDataTable = new YAHOO.widget.DataTable("datatableProduct", myColumnSetProduct, myDataSourceProduct, oConfigsProduct);
 myProductDataTable.subscribe("rowMouseoverEvent", myProductDataTable.onEventHighlightRow);
 myProductDataTable.subscribe("rowMouseoutEvent", myProductDataTable.onEventUnhighlightRow);
 myProductDataTable.subscribe("rowClickEvent", rowSelectedProduct);
 myProductDataTable.subscribe("renderEvent", function() {myProductDataTable.unselectAllRows();});
 myProductDataTable.selectRow(0);			        
	
 function addProductToResultProduct(id, name, price) {
	 $('#productId').val(id);
	 $('#productName').val(name);
	 $('#price').val(price);
	 $('#price').change();
	 return false;
  }
	    
  var ddProduct = new YAHOO.util.DD("productTablePopUp");
  ddProduct.setHandleElId("productTableTitle");
  ddProduct.addInvalidHandleType('input');
  $('#dt_input').val('');
	      
  $('#activateProductTable').click(function() {
    $('#myProductTable').toggle();
	return false;
  });
  $('#closeProductTable').click(function() {
	$('#myProductTable').toggle(); 
    $('dt_input').val('');
    return false;
  });
});
